package view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListModel;

import rmi.Discussion;
import rmi.Me;
import rmi.UserBuffer;

/**
 * @author REMY Timothée and CARTIER Arthur
 *
 */
public class MainWindow extends JFrame {
	public class WindowThread extends Thread {
		private final MainWindow mw;

		public WindowThread(final MainWindow mw) {
			super();
			this.mw = mw;
		}

		@Override
		public void run() {
			while (true) {
				mw.repaint();
				me.connectAllDisconnectedFriends();
				for (final Discussion d : me.getCurrentDiscussions()) {
					if (!discussionThatAreAlreadyDisplayed.contains(d)) {
						try {
							String discussionName = "";
							if (d != null && d.getParticipants() != null
									&& d.getParticipants().size() > 0) {
								discussionName = d.getParticipants().get(0)
										.getPseudo();
							}
							contactsList.addOneDiscussion(discussionName,
									new OneDiscussionTab(d, me));
						} catch (final RemoteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						discussionThatAreAlreadyDisplayed.add(d);
					}
				}
				try {
					Thread.sleep(2000);
				} catch (final InterruptedException e) {
					// this will not append
				}
			}
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1461921785127756290L;
	private Me me;
	private menuBar mb;
	private Clist contactsList;
	private JList blacklist;
	private JTabbedPane elemDiscuss;
	private final JTabbedPane lists;

	private ArrayList<Discussion> discussionThatAreAlreadyDisplayed;

	public MainWindow(final Me me) {
		super("Artim Messenger - " + me.getPseudo());
		final Dimension tailleEcran = Toolkit.getDefaultToolkit()
				.getScreenSize();
		setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setSize(new Dimension(800, 600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
		this.setLocation((tailleEcran.width - this.getSize().width) / 2,
				(tailleEcran.height - this.getSize().height) / 2);

		discussionThatAreAlreadyDisplayed = new ArrayList<Discussion>();
		this.me = me;
		elemDiscuss = new JTabbedPane();
		lists = new JTabbedPane();
		initMenuBar();
		initList();
		initTabbedPane();
		elemDiscuss.revalidate();
		contactsList.revalidate();
		mb.revalidate();
		this.repaint();
		final WindowThread wThread = new WindowThread(this);
		wThread.start();
	}

	public Clist getContactsList() {
		return contactsList;
	}

	/**
	 * @return the discussionTahtAreAlreadyDisplayed
	 */
	public ArrayList<Discussion> getDiscussionThatAreAlreadyDisplayed() {
		return discussionThatAreAlreadyDisplayed;
	}

	public JTabbedPane getElemDiscuss() {
		return elemDiscuss;
	}

	public menuBar getMb() {
		return mb;
	}

	public Me getMe() {
		return me;
	}

	public void initList() {
		final DefaultListModel listModel = new DefaultListModel();
		for (final UserBuffer u : me.getFriends().values()) {
			listModel.addElement(u);
		}
		final ListModel blacklistModel = new DefaultListModel();
		for (final String temp : me.getBlacklist()) {
			((DefaultListModel) blacklistModel).addElement(temp);
		}
		contactsList = new Clist(listModel, elemDiscuss, me, this);
		blacklist = new JList(blacklistModel);

		final JScrollPane contactScroll = new JScrollPane(contactsList);
		final JScrollPane blacklistScroll = new JScrollPane(blacklist);

		lists.add("Contacts", contactScroll);
		lists.add("Blacklist", blacklistScroll);
	}

	public void initMenuBar() {
		mb = new menuBar(me, this);
		mb.setVisible(true);
		setJMenuBar(mb);
	}

	public void initTabbedPane() {
		lists.setPreferredSize(new Dimension(getWidth() / 4, (getHeight()) - 60));
		elemDiscuss.setPreferredSize(new Dimension(getWidth()
				- (getWidth() / 4) - 25, getHeight() - 60));
		elemDiscuss.setVisible(true);
		lists.setVisible(true);
		elemDiscuss.revalidate();
		lists.revalidate();
		this.add(lists);
		this.add(elemDiscuss);
	}

	@Override
	public void repaint() {
		super.repaint();
		if (elemDiscuss != null) {
			elemDiscuss.repaint();
		}

		if (contactsList != null) {

			contactsList.getModelList().clear();
			for (final UserBuffer u : me.getFriends().values()) {
				contactsList.getModelList().addElement(u);
			}
			contactsList.repaint();
		}

		if (blacklist != null) {
			((DefaultListModel) blacklist.getModel()).clear();
			for (final String temp : me.getBlacklist()) {
				((DefaultListModel) blacklist.getModel()).addElement(temp);
			}
			blacklist.repaint();
		}
	}

	public void setContactsList(final Clist contactsList) {
		this.contactsList = contactsList;
	}

	/**
	 * @param discussionTahtAreAlreadyDisplayed
	 *            the discussionTahtAreAlreadyDisplayed to set
	 */
	public void setDiscussionThatAreAlreadyDisplayed(
			final ArrayList<Discussion> discussionTahtAreAlreadyDisplayed) {
		this.discussionThatAreAlreadyDisplayed = discussionTahtAreAlreadyDisplayed;
	}

	public void setElemDiscuss(final JTabbedPane elemDiscuss) {
		this.elemDiscuss = elemDiscuss;
	}

	public void setMb(final menuBar mb) {
		this.mb = mb;
	}

	public void setMe(final Me me) {
		this.me = me;
	}

}
